package com.practice.utils;

import lombok.Data;

import java.util.HashMap;

@Data
public class R extends HashMap<String, Object> {

//    //状态码
//    private Integer code;
//    //提示信息
//    private String msg;

    public R(int code, String msg) {
        put("code", code);
        put("msg", msg);
    }

    public R() {
        put("code", HttpCode.SUCCESS_OK);
        put("msg", "success");
    }

    public static R ok(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    public R setData(Object data) {
        put("data", data);
        return this;
    }

    public Object getData() {
        return this.get("data");
    }

    public static R ok(String msg) {
        return ok(HttpCode.SUCCESS_OK, msg);
    }

    public static R ok() {
        return new R();
    }

    public static R error() {
        return error(HttpCode.UNKNOWN_ERR, "未知异常");
    }

    public static R error(String msg) {
        return error(HttpCode.NESS_ERR, msg);
    }

    public static R error(int code, String msg) {
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }
}
